﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using WeiMiao;

namespace Code0914
{
    public class Panel : MonoBehaviour
    {
        [Header("该板子匹配的图形物体的名字")]
        public string targetName = "";

        [Header("该板子是否匹配的状态")]
        public bool isTarget = false;

        // 块物体原本归位的状态
        public GameObject targetObject;

        GameObject overallmusic;

        void Start()
        {
            overallmusic = GameObject.Find("OverallMusic");
        }

        private void OnTriggerEnter(Collider other)
        {
            if (other.gameObject.name == targetName)
            {           
                //Debug.Log("匹配：" + targetName);

                overallmusic.GetComponent<Music>().Sound(0);

                // 匹配状态
                isTarget = true;

                // 检测是否通关
                Random_Panel.GetInstance().CheckVictory();

                // 恢复移动块原本的位置
                other.transform.position = targetObject.transform.position;

                // 禁用匹配块的碰撞体，避免再次控制
                other.gameObject.GetComponent<BoxCollider>().enabled = false;
            }

            if (other.gameObject.name != targetName)
            {
                overallmusic.GetComponent<Music>().Sound(1);
            }
        }

        private void OnTriggerStay(Collider other)
        {
            if (other.gameObject.name == targetName)
            {
                isTarget = true;
            }
            else
            {
                isTarget = false;
            }
        }

        private void OnTriggerExit(Collider other)
        {
            if (other.gameObject.name == targetName)
            {
                isTarget = false;

                Random_Panel.GetInstance().CheckVictory();
            }
        }
    }
}
